package com.xsk.dou

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.xsk.dou.core.api.Action
import com.xsk.dou.core.api.ApiServer
import com.xsk.dou.core.api.NetWorkManager
import com.xsk.dou.databinding.ActivityMainBinding
import com.xsk.dou.model.Response
import com.xsk.dou.model.Subject
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {
    var activityMainBinding: ActivityMainBinding? = null
    var subjects: MutableList<Subject> = mutableListOf()
    var adapter: ListViewAdapter = ListViewAdapter(subjects, this)
    lateinit var pageInput: EditText
    lateinit var dispose: Disposable

    @SuppressLint("CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)
        activityMainBinding!!.adapter = adapter



        RxView.clicks(findViewById(R.id.clear)).throttleLast(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
//            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe({
                subjects.clear()
//                runOnUiThread({
                adapter.notifyDataSetChanged()
                activityMainBinding!!.subjects = subjects
//                })
            })

        pageInput = findViewById<EditText>(R.id.pageInput)
        RxTextView.textChanges(pageInput).debounce(500, TimeUnit.MILLISECONDS)////限流时间500ms
            .throttleLast(
                500,
                TimeUnit.MILLISECONDS
            )//最新的500ms内的最新变化，假如 500ms内输入3字符，则只返回一个整体，不会依次返回字符
            .map {
                if (it.length == 0) {
                    return@map 1
                }
                it.toString().toString().toInt()
            }.filter { it >= 1 }.subscribe({
                dispose = request(it)
            })
        dispose = request(1)


    }

    override fun onResume() {
        super.onResume()
        if (this.dispose != null && !dispose.isDisposed) {
            dispose.dispose()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (this.dispose != null && !dispose.isDisposed) {
            dispose.dispose()
        }
    }

    fun request(page: Int): Disposable {

        return NetWorkManager.manager.create(ApiServer::class.java,
            object : Action<List<Subject>, ApiServer> {
                override fun doAction(api: ApiServer): Observable<Response<List<Subject>>> {
                    return api.getMovies()
                }

            }).subscribe({
            subjects.addAll(it.subjects!!)
        }, {

        }, {
            adapter.notifyDataSetChanged()
            activityMainBinding!!.subjects = subjects

        })
    }

}
